import { Button, DatePicker, Input, NavBar } from 'antd-mobile'
import Icon from '@/components/Icon'
import './index.scss'
import classNames from 'classnames'
import { billListData } from '@/contants'
import { useNavigate } from 'react-router-dom'
import { useState } from 'react'
import dayjs from 'dayjs'
import { addBillList } from '@/store/modules/billStore'
import { useDispatch } from 'react-redux'

const New = () => {
  const navigate = useNavigate()

//准备控制支出、收入状态
const [type, setType] = useState('pay')   

// 保存账单
// 收集金额
const [money, setMoney] = useState()
const changeMoney = (value) => {
    setMoney(value)
}
// 账单类型
const [useFor, setUseFor] = useState('') 

const dispatch = useDispatch()
const saveBill = () => {
    const data = {
        type: type,
        money: type === 'income' ? +money : -money,
        date: pickDate,
        useFor: useFor
    }
    // console.log(data);
    dispatch(addBillList(data))
    navigate('/')
}

// 控制时间框的打开与关闭
const [visibile, setVisibile] = useState(false)
// 存储选择时间
const [pickDate, setPickDate] = useState(new Date())
const dateConfirm = (value) => {
    setVisibile(false)
    setPickDate(value)
}

  return (
    <div className="keepAccounts">
      <NavBar className="nav" onBack={() => navigate(-1)}>
        记一笔
      </NavBar>

      <div className="header">
        <div className="kaType">
          <Button
            shape="rounded"
            className={classNames( type === 'pay' && 'selected' )}
            onClick={() => setType('pay')}
          >
            支出
          </Button>
          <Button
            className={classNames( type === 'income' && 'selected' )}
            shape="rounded"
            onClick={() => setType('income')}
          >
            收入
          </Button>
        </div>

        <div className="kaFormWrapper">
          <div className="kaForm">
            <div className="date" onClick={() => setVisibile(true)}>
              <Icon type="calendar" className="icon" />
              <span className="text">{dayjs(pickDate).format('YYYY-MM-DD')}</span>
              <DatePicker
                className="kaDate"
                title="记账日期"
                max={new Date()}
                visible={visibile}
                onClose={() => setVisibile(false)}
                onConfirm={dateConfirm}
              />
            </div>
            <div className="kaInput">
              <Input
                className="input"
                placeholder="0.00"
                type="number"
                value={money}
                onChange={changeMoney}
              />
              <span className="iconYuan">¥</span>
            </div>
          </div>
        </div>
      </div>

      <div className="kaTypeList">
        {billListData[type].map(item => {
          return (
            <div className="kaType" key={item.type}>
              <div className="title">{item.name}</div>
              <div className="list">
                {item.list.map(item => {
                  return (
                    <div
                      className={classNames(
                        'item',
                        useFor === item.type && 'selected'
                      )}
                      key={item.type}
                      onClick={() => setUseFor(item.type)}
                    >
                      <div className="icon">
                        <Icon type={item.type} />
                      </div>
                      <div className="text">{item.name}</div>
                    </div>
                  )
                })}
              </div>
            </div>
          )
        })}
      </div>

      <div className="btns">
        <Button className="btn save" onClick={saveBill} >
          保 存
        </Button>
      </div>
    </div>
  )
}

export default New